﻿using DataBase.Utils;
using Prism.Commands;
using Prism.Mvvm;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using static GreenXi.Help.Curve.ClsJudge;

namespace GreenXi.Model
{
    public class SystemSettings : BindableBase
    {
        #region PLC
        private int _TCPPort = 8080;
        public int TCPPort
        {
            get { return _TCPPort; }
            set
            {
                _TCPPort = value;
                MyDbContext.SetSettingDbInfo("TCPPort", _TCPPort);
            }
        }

        private string _DataIP = "192.168.23.10";
        public string DataIP
        {
            get { return _DataIP; }
            set
            {
                _DataIP = value;
                MyDbContext.SetSettingDbInfo("DataIP", _DataIP);
            }
        }

        private int _DataPort = 102;
        public int DataPort
        {
            get { return _DataPort; }
            set
            {
                _DataPort = value;
                MyDbContext.SetSettingDbInfo("DataPort", _DataPort);
            }
        }

        private string _DataUser = "";
        public string DataUser
        {
            get { return _DataUser; }
            set
            {
                _DataUser = value;
                MyDbContext.SetSettingDbInfo("DataUser", _DataUser);
            }
        }

        private string _DataPassword = "";
        public string DataPassword
        {
            get { return _DataPassword; }
            set
            {
                _DataPassword = value;
                MyDbContext.SetSettingDbInfo("DataPassword", _DataPassword);
            }
        }

        #region PLC连接状态相关
        private bool isConnectedPLC = false;
        public bool IsConnectedPLC
        {
            get => isConnectedPLC;
            set
            {
                //PLC.IsConnected = isConnected;
                SetProperty(ref isConnectedPLC, value);
            }
        }

        //切换PLC连接状态
        public DelegateCommand? SwitchConnectStatusPLC { get; set; }
        #endregion

        #endregion

        #region 伺服电机(ServoMotor)
        #region 脉冲配置
        private int pulseConfiguration;

        public int PulseConfiguration
        {
            get { return pulseConfiguration; }
            set
            {
                pulseConfiguration = value;
                OnPropertyChanged(nameof(pulseConfiguration));
            }
        }

        #endregion

        //波特率
        private ObservableCollection<int>? _ServoMotorBaudRate;
        public ObservableCollection<int>? ServoMotorBaudRate
        {
            get => _ServoMotorBaudRate;
            set
            {
                _ServoMotorBaudRate = value;
                OnPropertyChanged(nameof(_ServoMotorBaudRate));
            }
        }

        private int _SelectedServoMotorBaudRate = 19200;
        public int SelectedServoMotorBaudRate
        {
            get { return _SelectedServoMotorBaudRate; }
            set
            {
                _SelectedServoMotorBaudRate = value;
                MyDbContext.SetSettingDbInfo("ServoMotorBaudRate", _SelectedServoMotorBaudRate);
            }
        }

        //数据位
        private int _ServoMotorDataBits = 8;
        public int ServoMotorDataBits
        {
            get { return _ServoMotorDataBits; }
            set
            {
                _ServoMotorDataBits = value;
                MyDbContext.SetSettingDbInfo("ServoMotorDataBits", _ServoMotorDataBits);
            }
        }

        //奇偶校验
        private ObservableCollection<string>? _ServoMotorParity;
        public ObservableCollection<string>? ServoMotorParity
        {
            get => _ServoMotorParity;
            set
            {
                _ServoMotorParity = value;
                OnPropertyChanged(nameof(_ServoMotorParity));
            }
        }

        private string _SelectedServoMotorParity = "Even";
        public string SelectedServoMotorParity
        {
            get { return _SelectedServoMotorParity; }
            set
            {
                _SelectedServoMotorParity = value;
                MyDbContext.SetSettingDbInfo("ServoMotorParity", _SelectedServoMotorParity);
            }
        }

        //停止位
        private ObservableCollection<string>? _ServoMotorStopBits;
        public ObservableCollection<string>? ServoMotorStopBits
        {
            get => _ServoMotorStopBits;
            set
            {
                _ServoMotorStopBits = value;
                OnPropertyChanged(nameof(_ServoMotorStopBits));
            }
        }

        private string _SelectedServoMotorStopBits = "One";
        public string SelectedServoMotorStopBits
        {
            get { return _SelectedServoMotorStopBits; }
            set
            {
                _SelectedServoMotorStopBits = value;
                MyDbContext.SetSettingDbInfo("ServoMotorStopBits", _SelectedServoMotorStopBits);
            }
        }

        //连接的串口名称
        private ObservableCollection<string>? _ServoMotorPortName1;
        public ObservableCollection<string>? ServoMotorPortName1
        {
            get => _ServoMotorPortName1;
            set
            {
                _ServoMotorPortName1 = value;
                OnPropertyChanged(nameof(_ServoMotorPortName1));
            }
        }

        private ObservableCollection<string>? _ServoMotorPortName2;
        public ObservableCollection<string>? ServoMotorPortName2
        {
            get => _ServoMotorPortName2;
            set
            {
                _ServoMotorPortName2 = value;
                OnPropertyChanged(nameof(_ServoMotorPortName2));
            }
        }

        private string _ServoMotorIp1 = "192.168.0.201";
        public string ServoMotorIp1
        {
            get { return _ServoMotorIp1; }
            set
            {
                _ServoMotorIp1 = value;
                MyDbContext.SetSettingDbInfo("ServoMotorIP1", _ServoMotorIp1);
            }
        }

        private string _ServoMotorIp2 = "192.168.0.202";
        public string ServoMotorIp2
        {
            get { return _ServoMotorIp2; }
            set
            {
                _ServoMotorIp2 = value;
                MyDbContext.SetSettingDbInfo("ServoMotorIP2", _ServoMotorIp2);
            }
        }

        private string _SelectedServoMotorPortName1 = "COM4";
        public string SelectedServoMotorPortName1
        {
            get { return _SelectedServoMotorPortName1; }
            set
            {
                _SelectedServoMotorPortName1 = value;
                MyDbContext.SetSettingDbInfo("ServoMotorPortName1", _SelectedServoMotorPortName1);
            }
        }

        private string _SelectedServoMotorPortName2 = "COM5";
        public string SelectedServoMotorPortName2
        {
            get { return _SelectedServoMotorPortName2; }
            set
            {
                _SelectedServoMotorPortName2 = value;
                MyDbContext.SetSettingDbInfo("ServoMotorPortName2", _SelectedServoMotorPortName2);
            }
        }

        //slave设备地址
        private int _ServoMotorSlaveAddress = 0;
        public int ServoMotorSlaveAddress
        {
            get { return _ServoMotorSlaveAddress; }
            set
            {
                _ServoMotorSlaveAddress = value;
                MyDbContext.SetSettingDbInfo("ServoMotorSlaveAddress", _ServoMotorSlaveAddress);
            }
        }

        //获取位移的地址
        private int _ServoMotorPosAds = 18;
        public int ServoMotorPosAds
        {
            get { return _ServoMotorPosAds; }
            set
            {
                _ServoMotorPosAds = value;
                MyDbContext.SetSettingDbInfo("ServoMotorPosAds", _ServoMotorPosAds);
            }
        }

        #region 伺服电机连接状态相关
        private bool isConnectedServoMotor = false;
        public bool IsConnectedServoMotor
        {
            get => isConnectedServoMotor;
            set
            {
                //ServoMotor.IsConnected = isConnected;
                SetProperty(ref isConnectedServoMotor, value);
            }
        }

        //切换PLC连接状态
        public DelegateCommand? SwitchConnectStatusServoMotor { get; set; }
        #endregion
        #endregion

        #region 数据采集卡(DataAcquisitionCard)
        //波特率
        private ObservableCollection<int>? _DataAcquisitionCardBaudRate;
        public ObservableCollection<int>? DataAcquisitionCardBaudRate
        {
            get => _DataAcquisitionCardBaudRate;
            set
            {
                _DataAcquisitionCardBaudRate = value;
                OnPropertyChanged(nameof(_DataAcquisitionCardBaudRate));
            }
        }

        private int _SelectedDataAcquisitionCardBaudRate = 19200;
        public int SelectedDataAcquisitionCardBaudRate
        {
            get { return _SelectedDataAcquisitionCardBaudRate; }
            set
            {
                _SelectedDataAcquisitionCardBaudRate = value;
                MyDbContext.SetSettingDbInfo("DataAcquisitionCardBaudRate", _SelectedDataAcquisitionCardBaudRate);
            }
        }

        //数据位
        private int _DataAcquisitionCardDataBits = 8;
        public int DataAcquisitionCardDataBits
        {
            get { return _DataAcquisitionCardDataBits; }
            set
            {
                _DataAcquisitionCardDataBits = value;
                MyDbContext.SetSettingDbInfo("DataAcquisitionCardDataBits", _DataAcquisitionCardDataBits);
            }
        }

        //奇偶校验
        private ObservableCollection<string>? _DataAcquisitionCardParity;
        public ObservableCollection<string>? DataAcquisitionCardParity
        {
            get => _DataAcquisitionCardParity;
            set
            {
                _DataAcquisitionCardParity = value;
                OnPropertyChanged(nameof(_DataAcquisitionCardParity));
            }
        }

        private string _SelectedDataAcquisitionCardParity = "Even";
        public string SelectedDataAcquisitionCardParity
        {
            get { return _SelectedDataAcquisitionCardParity; }
            set
            {
                _SelectedDataAcquisitionCardParity = value;
                MyDbContext.SetSettingDbInfo("DataAcquisitionCardParity", _SelectedDataAcquisitionCardParity);
            }
        }

        //停止位
        private ObservableCollection<string>? _DataAcquisitionCardStopBits;
        public ObservableCollection<string>? DataAcquisitionCardStopBits
        {
            get => _DataAcquisitionCardStopBits;
            set
            {
                _DataAcquisitionCardStopBits = value;
                OnPropertyChanged(nameof(_DataAcquisitionCardStopBits));
            }
        }

        private string _SelectedDataAcquisitionCardStopBits = "One";
        public string SelectedDataAcquisitionCardStopBits
        {
            get { return _SelectedDataAcquisitionCardStopBits; }
            set
            {
                _SelectedDataAcquisitionCardStopBits = value;
                MyDbContext.SetSettingDbInfo("DataAcquisitionCardStopBits", _SelectedDataAcquisitionCardStopBits);
            }
        }

        //连接的串口名称
        private ObservableCollection<string>? _DataAcquisitionCardPortNameList;
        public ObservableCollection<string>? DataAcquisitionCardPortNameList
        {
            get => _DataAcquisitionCardPortNameList;
            set
            {
                _DataAcquisitionCardPortNameList = value;
                OnPropertyChanged(nameof(_DataAcquisitionCardPortNameList));
            }
        }

        private string _SelectedDataAcquisitionCardPortName = "COM5";
        public string SelectedDataAcquisitionCardPortName
        {
            get { return _SelectedDataAcquisitionCardPortName; }
            set
            {
                _SelectedDataAcquisitionCardPortName = value;
                MyDbContext.SetSettingDbInfo("DataAcquisitionCardPortName", _SelectedDataAcquisitionCardPortName);
            }
        }

        //slave设备地址
        private int _DataAcquisitionCardSlaveAddress = 1;
        public int DataAcquisitionCardSlaveAddress
        {
            get { return _DataAcquisitionCardSlaveAddress; }
            set
            {
                _DataAcquisitionCardSlaveAddress = value;
                MyDbContext.SetSettingDbInfo("DataAcquisitionCardSlaveAddress", _DataAcquisitionCardSlaveAddress);
            }
        }

        //获取压力的地址
        private int _DataAcquisitionCardPressAds = 18;
        public int DataAcquisitionCardPressAds
        {
            get { return _DataAcquisitionCardPressAds; }
            set
            {
                _DataAcquisitionCardPressAds = value;
                MyDbContext.SetSettingDbInfo("DataAcquisitionCardPosAds", _DataAcquisitionCardPressAds);
            }
        }
        #endregion

        #region 运动控制卡
        //运动控制卡卡号
        private int _CardNum1 = 1;
        public int CardNum1
        {
            get { return _CardNum1; }
            set
            {
                _CardNum1 = value;
                MyDbContext.SetSettingDbInfo("CardNum", _CardNum1);
            }
        }

        private int _CardNum2 = 2;
        public int CardNum2
        {
            get { return _CardNum2; }
            set
            {
                _CardNum2 = value;
                MyDbContext.SetSettingDbInfo("CardNum", _CardNum2);
            }
        }

        //PC本机IP（默认请设置到192.168.0.200）
        private string _PCEthernetIP = "192.168.0.200";
        public string PCEthernetIP
        {
            get { return _PCEthernetIP; }
            set
            {
                _PCEthernetIP = value;
                MyDbContext.SetSettingDbInfo("PCEthernetIP", _PCEthernetIP);
            }
        }

        //PC->运动控制卡端口号
        private int _PCEthernetPort = 60000;
        public int PCEthernetPort
        {
            get { return _PCEthernetPort; }
            set
            {
                _PCEthernetPort = value;
                MyDbContext.SetSettingDbInfo("PCEthernetPort", _PCEthernetPort);
            }
        }

        //运动控制卡IP（默认请设置到192.168.0.1）
        private string _CardEthernetIP = "192.168.0.1";
        public string CardEthernetIP
        {
            get { return _CardEthernetIP; }
            set
            {
                _CardEthernetIP = value;
                MyDbContext.SetSettingDbInfo("CardEthernetIP", _CardEthernetIP);
            }
        }

        //运动控制卡->PC端口号
        private int _CardEthernetPort = 60000;
        public int CardEthernetPort
        {
            get { return _CardEthernetPort; }
            set
            {
                _CardEthernetPort = value;
                MyDbContext.SetSettingDbInfo("CardEthernetPort", _CardEthernetPort);
            }
        }
        #endregion

        #region 限制设置
        #region 最大位移（maximum displacement）ComboBox
        private ObservableCollection<int>? _MaximumDisplacement;
        public ObservableCollection<int>? MaximumDisplacement
        {
            get => _MaximumDisplacement;
            set
            {
                _MaximumDisplacement = value;
                OnPropertyChanged(nameof(_MaximumDisplacement));
            }
        }

        private int selectedMaximumDisplacement = 1;
        public int SelectedMaximumDisplacement
        {
            get { return selectedMaximumDisplacement; }
            set
            {
                selectedMaximumDisplacement = value;
                MyDbContext.SetSettingDbInfo("MaximumDisplacement", selectedMaximumDisplacement);
            }
        }
        #endregion

        #region 最小位移（MinimumDisplacement）ComboBox
        private ObservableCollection<int>? _MinimumDisplacement;
        public ObservableCollection<int>? MinimumDisplacement
        {
            get => _MinimumDisplacement;
            set
            {
                _MinimumDisplacement = value;
                OnPropertyChanged(nameof(_MinimumDisplacement));
            }
        }

        private int selectedMinimumDisplacement = 1;
        public int SelectedMinimumDisplacement
        {
            get { return selectedMinimumDisplacement; }
            set
            {
                selectedMinimumDisplacement = value;
                MyDbContext.SetSettingDbInfo("MinimumDisplacement", selectedMinimumDisplacement);
            }
        }
        #endregion

        #region 最大压力（greatest pressure）ComboBox
        private ObservableCollection<int>? _GreatestPressure;
        public ObservableCollection<int>? GreatestPressure
        {
            get => _GreatestPressure;
            set
            {
                _GreatestPressure = value;
                OnPropertyChanged(nameof(_GreatestPressure));
            }
        }

        private int selectedGreatestPressure = 1;
        public int SelectedGreatestPressure
        {
            get { return selectedGreatestPressure; }
            set
            {
                selectedGreatestPressure = value;
                GlobalVariables.GreatestPressure = selectedGreatestPressure;
                MyDbContext.SetSettingDbInfo<int>("GreatestPressure", selectedGreatestPressure);
            }
        }
        #endregion

        #region 最大速度（Maximum speed）ComboBox
        private ObservableCollection<int>? _MaximumSpeed;
        public ObservableCollection<int>? MaximumSpeed
        {
            get => _MaximumSpeed;
            set
            {
                _MaximumSpeed = value;
                OnPropertyChanged(nameof(_MaximumSpeed));
            }
        }

        private int selectedMaximumSpeed = 1;
        public int SelectedMaximumSpeed
        {
            get
            {
                return selectedMaximumSpeed;
            }
            set
            {
                selectedMaximumSpeed = value;
                MyDbContext.SetSettingDbInfo<int>("MaximumSpeed", selectedMaximumSpeed);
            }
        }
        #endregion

        #region 小点动速度（Small Jog Speed）ComboBox
        private ObservableCollection<double>? _SmallJogSpeed;
        public ObservableCollection<double>? SmallJogSpeedList
        {
            get => _SmallJogSpeed;
            set
            {
                _SmallJogSpeed = value;
                OnPropertyChanged(nameof(_SmallJogSpeed));
            }
        }

        private double _SelectedSmallJogSpeed = 1;
        public double SelectedSmallJogSpeed
        {
            get { return _SelectedSmallJogSpeed; }
            set
            {
                _SelectedSmallJogSpeed = value;
                MyDbContext.SetSettingDbInfo("SmallJogSpeed", _SelectedSmallJogSpeed);
            }
        }
        #endregion

        #region 小点动速度（Big Jog Speed）ComboBox
        private ObservableCollection<double>? _BigJogSpeed;
        public ObservableCollection<double>? BigJogSpeedList
        {
            get => _BigJogSpeed;
            set
            {
                _BigJogSpeed = value;
                OnPropertyChanged(nameof(_BigJogSpeed));
            }
        }

        private double _SelectedBigJogSpeed = 1;
        public double SelectedBigJogSpeed
        {
            get { return _SelectedBigJogSpeed; }
            set
            {
                _SelectedBigJogSpeed = value;
                MyDbContext.SetSettingDbInfo("BigJogSpeed", _SelectedBigJogSpeed);
            }
        }
        #endregion

        #region 加速度（Acceleration）ComboBox
        private ObservableCollection<double>? _Acceleration;
        public ObservableCollection<double>? AccelerationList
        {
            get => _Acceleration;
            set
            {
                _Acceleration = value;
                OnPropertyChanged(nameof(_Acceleration));
            }
        }

        private double _SelectedAcceleration = 1;
        public double SelectedAcceleration
        {
            get { return _SelectedAcceleration; }
            set
            {
                _SelectedAcceleration = value;
                MyDbContext.SetSettingDbInfo("Acceleration", _SelectedAcceleration);
            }
        }
        #endregion

        #region 减速度（Deceleration）ComboBox
        private ObservableCollection<double>? _Deceleration;
        public ObservableCollection<double>? DecelerationList
        {
            get => _Deceleration;
            set
            {
                _Deceleration = value;
                OnPropertyChanged(nameof(_Deceleration));
            }
        }

        private double _SelectedDeceleration = 1;
        public double SelectedDeceleration
        {
            get { return _SelectedDeceleration; }
            set
            {
                _SelectedDeceleration = value;
                MyDbContext.SetSettingDbInfo("Deceleration", _SelectedDeceleration);
            }
        }
        #endregion

        #endregion

        #region 基础设置
        #region 原点
        private ObservableCollection<int> _OriginList;
        public ObservableCollection<int> OriginList
        {
            get => _OriginList;
            set { SetProperty(ref _OriginList, value); }
        }

        private int _SelectedOrigin = 4;
        public int SelectedOrigin
        {
            get
            {
                return _SelectedOrigin;
            }
            set
            {
                _SelectedOrigin = value;
                MyDbContext.SetSettingDbInfo<int>("Origin", _SelectedOrigin);
            }
        }
        #endregion

        #region 压机数量（compressors number）ComboBox

        private ObservableCollection<int> _CompressorsNumber;
        public ObservableCollection<int> CompressorsNumber
        {
            get => _CompressorsNumber;
            set { SetProperty(ref _CompressorsNumber, value); }
        }

        private int selectedCompressorsNumber = 1;
        public int SelectedCompressorsNumber
        {
            get
            {
                selectedCompressorsNumber = int.Parse(MyDbContext.Instance.Settings.FirstOrDefault(x => x.Key == "CompressorsNumber").Value);
                GlobalVariables.CompressorsNumber = selectedCompressorsNumber;
                return selectedCompressorsNumber;
            }
            set
            {
                selectedCompressorsNumber = value;
                GlobalVariables.CompressorsNumber = selectedCompressorsNumber;
                MyDbContext.SetSettingDbInfo("CompressorsNumber", selectedCompressorsNumber);
            }
        }

        #endregion

        #region 小数位数
        private ObservableCollection<int> _DecimalPlaces;
        public ObservableCollection<int> DecimalPlaces
        {
            get => _DecimalPlaces;
            set { SetProperty(ref _DecimalPlaces, value); }
        }

        private int selectedDecimalPlaces = 0;
        public int SelectedDecimalPlaces
        {
            get
            {
                selectedDecimalPlaces = int.Parse(MyDbContext.Instance.Settings.FirstOrDefault(x => x.Key == "DecimalPlaces").Value);
                return selectedDecimalPlaces;
            }
            set
            {
                selectedDecimalPlaces = value;
                MyDbContext.SetSettingDbInfo<int>("DecimalPlaces", selectedDecimalPlaces);
            }
        }
        #endregion

        

        

        #region 伺服1下限
        private double _PositiveBottom1;
        public double PositiveBottom1
        {
            get => _PositiveBottom1;
            set
            {
                SetProperty(ref _PositiveBottom1, value);
                MyDbContext.SetSettingDbInfo("PositiveBottom1", value);
            }
        }
        #endregion

      

        #region 伺服2下限
        private double _PositiveBottom2;
        public double PositiveBottom2
        {
            get => _PositiveBottom2;
            set
            {
                SetProperty(ref _PositiveBottom2, value);
                MyDbContext.SetSettingDbInfo("PositiveBottom2", value);
            }
        }
        #endregion

        #region 伺服2上限
        private double _PositiveTop2;
        public double PositiveTop2
        {
            get => _PositiveTop2;
            set
            {
                SetProperty(ref _PositiveTop2, value);
                MyDbContext.SetSettingDbInfo("PositiveTop2", value);
            }
        }
        #endregion

        #endregion

        #region 语言
        //语言
        private bool? _LanguageCn;

        public bool? LanguageCn
        {
            get => _LanguageCn;
            set { SetProperty(ref _LanguageCn, value); }
        }

        private bool _LanguageEn;

        public bool LanguageEn
        {
            get => _LanguageEn;
            set { SetProperty(ref _LanguageEn, value); }
        }

        #endregion

        #region 操作定义

        //切换语言
        public DelegateCommand SwitchLanguage { get; set; }

        //压机数量选择完成
        public DelegateCommand CompressorsNumberChangeCommand { get; set; }

        //寻零
        public DelegateCommand<object> FindZero { get; set; }
        //设置正极限
        public DelegateCommand<object> SetPositiveLimit { get; set; }
        //正极限按钮释放
        public DelegateCommand<object> SetPositiveLimitPreviewMouse { get; set; }
        //设置负极限
        public DelegateCommand<object> SetNegativeLimit { get; set; }
    
        #endregion

        #region 页面显示相关
        private string displayVisibility = "Collapsed";
        public string DisplayVisibility
        {
            get { return displayVisibility; }
            set { SetProperty(ref displayVisibility, value); }
        }
        #endregion

        #region INotifyPropertyChanged相关
        public event PropertyChangedEventHandler? PropertyChanged;

        public void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
}
